<template>
   <el-container class="home-container">
       <!-- 头部区域 -->
     <el-header class="el-header">
         <div>
             <img src="../assets/heima.png" alt="">
             <span>电商后台管理系统</span>
         </div>
         <el-button type="info" @click="logout">退出</el-button>
     </el-header>
     <!-- 页面主题区域 -->
     <el-container>
         <!-- 页面左侧区域 -->
        <el-aside :width='changeCollapse?64+"px":200+"px"' class="el-aside">
            <div class="toggle-button" @click="changeButton">|||</div>
            <el-menu background-color="#333744" text-color="#fff"
             active-text-color="#409EFF" unique-opened :collapse="changeCollapse" 
             :collapse-transition="false" router :default-active="active">
                <!-- 一级菜单 -->                                                                                                                                                                                                                                
                <el-submenu :index='item.id+""' v-for="item in menuList" :key="item.id">
                    <!-- 一级菜单模板区域 -->
                    <template slot="title">
                        <i :class="icos[item.id]"></i>
                        <span>{{item.authName}}</span>
                    </template>
                    <!-- 二级菜单 -->
                    <el-menu-item :index='"/"+itemChidren.path' v-for="itemChidren in item.children" :key="itemChidren.id" @click="changeActive(itemChidren.path)">
                        <template slot="title">
                        <i class="el-icon-menu"></i>
                        <span>{{itemChidren.authName}}</span>
                    </template>
                    </el-menu-item>
                </el-submenu>
            </el-menu>
        </el-aside>
        <!-- 页面右侧区域 -->
        <el-main class="el-main">
            <router-view></router-view>
        </el-main>
     </el-container>
    </el-container>
</template>
    
<script>
export default {
    created () {
        this.getMenuList();
        // 当页面加载的时候 将sessionStorage里面的值赋值给属性变量
        this.active=window.sessionStorage.getItem('active');
    },
    data () {
        return {
            menuList:[],
            icos:{
               "125": 'iconfont icon-users',
               "103": 'iconfont icon-tijikongjian',
               "101": 'iconfont icon-shangpin',
               "102": 'iconfont icon-danju',
               "145": 'iconfont icon-baobiao' 
            },
            changeCollapse :false,
            active:''
        }
    },
    methods: {
        logout: function() {
            window.sessionStorage.clear();
            this.$message.success('退出成功');
            this.$router.push('/login');
        },
        getMenuList: async function () {
          var {data:res} =await this.$http.get('menus');
          if(res.meta.status==200){
              this.menuList=res.data;
          }
          else {
            this.$message.error(res.meta.msg);
              return ;
          }
        },
        changeButton: function () { 
                this.changeCollapse=!this.changeCollapse;
         },
        //  实现左侧点击图标高亮显示
        changeActive: function(active) {
            var path='/'+active;
            window.sessionStorage.setItem('active',path);
            this.active=path;
        }
    }
}
</script>

<style lang="less" scoped>
.home-container {
    height: 100%;
}
.el-header {
    background: #373d41;
    display: flex;
    justify-content: space-between;
    padding-left: 0;
    align-items: center;
    color: #fff;
    font-size: 20px;
    div {
        display: flex;
        align-items: center;
        span {
            margin-left: 15px;
        }
    }
}
.el-aside {
    background: #333744;
    .el-menu {
        border-right: 0;
    }
}
.el-main {
    background: #eaedf1;
}
.iconfont {
    margin-right: 10px;
}
.toggle-button {
    font-size: 15px;
    line-height: 24px;
    background-color: #4a5064;
    color: #fff;
    text-align: center;
    letter-spacing: 0.2em;
    cursor: pointer;

}
</style>
